package b3.model
{
	import b3.net.*;
	import b3.util.*;
	
	public class RankList extends Model
	{
		public var ranks:Array;
		
		public function RankList()
		{
			super();
		}
		
		private function parseRankList(data:Array):void
		{
			ranks = new Array();
			for (var i:int = 0; i < data.length; i++)
			{
				var rankArray:Array = new Array();
				for (var renrenId:String in data[i])
				{
					var rankInfo:RankInfo = new RankInfo();
					rankInfo.loadbyObject(data[i][renrenId]);
					rankArray.push(rankInfo);
				}
				rankArray.sort(compareRankInfo);
				ranks.push(rankArray);
			}
		}
		
		private function compareRankInfo(a:RankInfo, b:RankInfo):int
		{
			if (a.ranking < b.ranking)
				return -1;
			else if (a.ranking > b.ranking)
				return 1;
			else
				return 0;
		}
		
		public function loadTotalRankList(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_RANK_LIST, {xn_sig_session_key:Profile.xn_sig_session_key});
			serverRequest.setHandleFunc(handleTotalRankList);
			serverRequest.post(force);
		}
		
		public function handleTotalRankList(data:Object):void
		{
			parseRankList(data as Array);
			dispatchEvent(new ModelEvent(ModelEvent.REFRESH_RANK_LIST));
		}
		
	}
}